package com.example.demo.controller;

import com.example.demo.pojo.RoomType;
import com.example.demo.service.RoomTypeService;
import com.example.demo.utils.SelectDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@ComponentScan
public class RoomTypeController {

    @Autowired
    RoomTypeService roomTypeService;
    @Autowired
    SelectDate selectDate;
    @Transactional
    @GetMapping("/Admin/getRoomTypeByName")
    @ResponseBody
    public RoomType getRoomTypeByName(String typeName, Model model){
        RoomType roomType = roomTypeService.getRoomTypeByName("typeName");
        model.addAttribute("roomtype",roomType);
        return roomType;
    }
    @Transactional
    @GetMapping("/Admin/addRoomType")
    @ResponseBody
    public String addRoomType( String typeName, int roomPrice, int roomArea, int rateNum, int rateBed){

        int flag = roomTypeService.addRoomType(typeName,roomPrice,roomArea,rateNum,rateBed);

        if (flag ==1)
            return "添加成功";
        else
            return "添加失败";
    }
    @Transactional
    @GetMapping("/Admin/updateRoomPriceByTypeName")
    @ResponseBody
    public String updateRoomPriceByTypeName( String typeName, int roomPrice){

        int flag = roomTypeService.updateRoomPriceByTypeName(typeName,roomPrice);

        if (flag ==1)
            return "修改成功";
        else
            return "修改失败";
    }

    /**
     * 删除表格前弹出确认删除框
     * @param
     * @return
     */
    @Transactional
    @GetMapping("/Admin/deleteRoomTypeByTypeName")
    @ResponseBody
    public String deleteRoomTypeByTypeName( String typeName){


        int flag = roomTypeService.deleteRoomTypeByTypeName(typeName);

        if (flag ==1)
            return "删除成功";
        else
            return "删除失败";
    }
    @Transactional
    @RequestMapping(value = "/user-lookRoomType",method = RequestMethod.GET)
    public String getlookRoomType(Model model){
        List<RoomType>roomTypes = roomTypeService.getRoomTypeAll();
        model.addAttribute("roomTypes",roomTypes);
        return "user-lookRoomType";
    }

    /**
     * 日期选择显示房型
     */
    @Transactional
    @RequestMapping(value = "/user-selectRoomType",method = RequestMethod.GET)
    public String getroomtype(@RequestParam("arriveDate")String arriveDate, @RequestParam("leaveDate")String leaveDate, Model model, HttpSession session){
       int typeID;
       int sum;
        List<RoomType>roomTypes = roomTypeService.getRoomTypeAll();
        if (arriveDate==""||leaveDate==""){
            model.addAttribute("warn","请先选择日期");
            return "user-lookRoomType";
        }else {
            for( RoomType roomType:roomTypes){
                typeID= roomType.getTypeID();
                sum= selectDate.selectdate(arriveDate,leaveDate,typeID);
                roomType.setCount(roomType.getRoomNum()-sum);
            }
            model.addAttribute("roomTypes",roomTypes);
            session.setAttribute("arriveDate",arriveDate);
            session.setAttribute("leaveDate",leaveDate);

            return "user-selectRoomType";
        }
    }
}
